import datetime

from module_admin.dao.cms_car_dao import CmsCarDao
from module_admin.dao.cms_driver_dao import CmsDriverDao
from module_admin.dao.cms_order_approve_dao import CmsOrderApproveDao
from module_admin.dao.cms_order_bill_dao import CmsOrderBillDao
from module_admin.dao.cms_order_dao import CmsOrderDao
from module_admin.dao.cms_order_dispatch_dao import CmsOrderDispatchDao
from module_admin.dao.cms_order_execute_dao import CmsOrderExecuteDao
from module_admin.dao.dict_dao import DictDataDao, DictTypeDao
from module_admin.entity.do.cms_car import CmsCar
from module_admin.entity.do.cms_driver import CmsDriver
from module_admin.entity.do.cms_order import CmsOrder
from module_admin.dao.dept_dao import *
from module_admin.entity.do.cms_order_approve import CmsOrderApprove
from module_admin.entity.do.cms_order_bill import CmsOrderBill
from module_admin.entity.do.cms_order_dispatch import CmsOrderDispatch
from module_admin.entity.do.cms_order_execute import CmsOrderExecute
from module_admin.entity.vo.cms_driver_vo import DriverModel
from module_admin.entity.vo.cms_order_approve_vo import OrderApproveModel
from module_admin.entity.vo.cms_order_bill_vo import OrderBillModel
from module_admin.entity.vo.cms_order_dispatch_vo import OrderDetails, OrderDispatchModel, DriverExecuteModel, \
    OrderDispatchModel2
from module_admin.entity.vo.cms_order_execute_vo import OrderExecuteModel
from module_admin.entity.vo.cms_order_vo import OrderModel, CrudOrderResponse, OrderModelForQuery, OrderPageObject
from module_admin.enum.state_enum import OrderStateEnum, OrderDispatchStateEnum
from utils.stringUtils import stringUtils
from utils.time_format_util import object_format_datetime
from utils.uuid import UUID


class CmsOrderService:
    """
    部门管理模块服务层
    """

    @classmethod
    def create_order(cls, result_db: Session, page_object: OrderModel):
        destination = stringUtils.get_one(page_object.destination_list)
        # 待审核
        page_object.state = OrderStateEnum.dsh.value
        page_object.order_id = "ord_" + UUID().uuid()
        try:
            CmsOrderDao.create_order(result_db, page_object, destination)
            result_db.commit()
            result = dict(is_success=True, message='新增成功')
        except Exception as e:
            result_db.rollback()
            result = dict(is_success=False, message=str(e))

        return CrudOrderResponse(**result)

    @classmethod
    def delete_order(cls, query_db: Session, order_id: int):
        CmsOrderDao.delete_order_by_id(query_db, order_id)
        query_db.commit()

    @classmethod
    def update_order(cls, db, order_update: OrderModel):
        order_update_dict = order_update.dict(exclude_unset=True)
        destination = stringUtils.get_one(order_update.destination_list)
        order_update_dict.setdefault("destination", destination)
        if "destination_list" in order_update_dict:
            del order_update_dict["destination_list"]
        if "start_time" in order_update_dict:
            del order_update_dict["start_time"]
        if "end_time" in order_update_dict:
            del order_update_dict["end_time"]
        if "use_car_type_name" in order_update_dict:
            del order_update_dict["use_car_type_name"]
        if "car_model_name" in order_update_dict:
            del order_update_dict["car_model_name"]
        if "car_type_name" in order_update_dict:
            del order_update_dict["car_type_name"]
        try:
            CmsOrderDao.update_order_by_id(db, order_update_dict)
            db.commit()
            result = dict(is_success=True, message='更新成功')
        except Exception as e:
            db.rollback()
            result = dict(is_success=False, message=str(e))
        return CrudOrderResponse(**result)

    @classmethod
    def get_order(cls, query_db: Session, id: int):
        order_by_id: CmsOrder = CmsOrderDao.get_order_by_id(query_db, id)
        order_dict = cls.del_and_add_destnation(order_by_id)
        # 用车类型-中文
        DictDataDao.chang_data_value_to_code(query_db, "yongcheleixing", "use_car_type", order_dict["use_car_type"],
                                             order_dict)
        DictDataDao.chang_data_value_to_code(query_db, "chexing", "car_model", order_dict["car_model"],
                                             order_dict)
        DictDataDao.chang_data_value_to_code(query_db, "cheliangleixing", "car_type", order_dict["car_type"],
                                             order_dict)
        return OrderModel(**order_dict)

    @classmethod
    def del_and_add_destnation(cls, order_by_id: CmsOrder):
        if order_by_id is None:
            return
        destination_list = stringUtils.get_list(order_by_id.destination)
        order_dict = order_by_id.__dict__
        if "destination" in order_dict:
            del order_dict["destination"]
        order_dict.setdefault("destination_list", destination_list)
        return order_dict

    @classmethod
    def list(cls, db: Session, query_object: OrderModelForQuery):
        order_list_result = CmsOrderDao.list(db, query_object)
        return order_list_result

    @classmethod
    def page(cls, db: Session, query_object: OrderPageObject):
        order_list_result = CmsOrderDao.page(db, query_object)
        return order_list_result

    @classmethod
    def count(cls, db: Session, query_object: OrderPageObject):
        order_list_result = CmsOrderDao.count(db, query_object)
        return order_list_result

    @classmethod
    def detail(cls, db: Session, id: int):
        order_details: OrderDetails = OrderDetails()
        # 用车信息#
        order: CmsOrder = CmsOrderDao.get_order_by_id(db, id)
        order_dict = CmsOrderService.del_and_add_destnation(order)
        # 用车类型-中文
        if order_dict:
            DictDataDao.chang_data_value_to_code(db, "yongcheleixing", "use_car_type", order_dict["use_car_type"],
                                                 order_dict)
            DictDataDao.chang_data_value_to_code(db, "chexing", "car_model", order_dict["car_model"],
                                                 order_dict)
            DictDataDao.chang_data_value_to_code(db, "cheliangleixing", "car_type", order_dict["car_type"],
                                                 order_dict)
        order_model = OrderModel(**order_dict)
        object_format_datetime(order_model)
        order_details.order_details = order_model
        # 审批信息
        order_approve: CmsOrderApprove = CmsOrderApproveDao.get_one_by_order_id(db, order.order_id)
        if order_approve is not None:
            order_approve_model = OrderApproveModel(**vars(order_approve))
            order_approve_model.result = "pass" if order_approve_model.state == 1 else "refuse"
            object_format_datetime(order_approve_model)
            order_details.order_approve_details = order_approve_model
        # 调度信息
        order_dispatch: CmsOrderDispatch = CmsOrderDispatchDao.get_one_by_order_id(db, order.order_id)
        if order_dispatch is not None:
            order_dispatch_model = OrderDispatchModel2(**vars(order_dispatch))
            object_format_datetime(order_dispatch_model)
            # 驾驶员信息
            driver: CmsDriver = CmsDriverDao.get_one_by_driver_id(db, order_dispatch.driver_id)
            if driver is not None:
                order_dispatch_model.driver_name = driver.driver_name
                order_dispatch_model.driver_phone = driver.phone
            car: CmsCar = CmsCarDao.get_car_by_car_id(db, order_dispatch.car_id)
            if car is not None:
                order_dispatch_model.car_type = car.car_type
                order_dispatch_model.car_number = car.car_number
            order_details.order_dispatch_details = order_dispatch_model
            # 出车信息
            driver_execute_model = DriverExecuteModel()
            driver_execute_model.state = order_dispatch.state
            driver_execute_model.state_name = OrderDispatchStateEnum.get_state_name(order_dispatch.state)
            driver_execute_model.remark = order_dispatch.reason
            driver_execute_model.operate_time = order_dispatch.confirm_time
            driver_execute_model.confirm_time = order_dispatch.confirm_time
            order_details.driver_details = driver_execute_model
        order_execute: CmsOrderExecute = CmsOrderExecuteDao.get_one_by_order_id(db, order.order_id)
        if order_execute is not None:
            order_execute_model = OrderExecuteModel(**vars(order_execute))
            object_format_datetime(order_execute_model)
            order_details.order_execute_details = order_execute_model
        # 结算信息
        order_bill: CmsOrderBill = CmsOrderBillDao.get_one_by_order_id(db, order.order_id)
        if order_bill is not None:
            order_bill_model = OrderBillModel(**vars(order_bill))
            object_format_datetime(order_bill_model)
            order_execute_model = order_details.order_execute_details
            order_execute_model.expenses = order_bill_model.expenses
            order_execute_model.sheet_expenses = order_bill_model.sheet_expenses
            order_details.order_execute_details = order_execute_model
            order_details.order_bill_details = order_bill_model
        return order_details

    @classmethod
    def list2(cls, db, query_object):
        order_list_result = CmsOrderDao.list2(db, query_object)
        return order_list_result

    @classmethod
    def page2(cls, db, query_object: OrderPageObject):
        order_list_result = CmsOrderDao.page2(db, query_object)
        return order_list_result

    @classmethod
    def count2(cls, db, query_object: OrderPageObject):
        order_list_result = CmsOrderDao.count2(db, query_object)
        return order_list_result
